package com.briup.estore.Service;

import com.briup.estore.bean.Order;
import com.briup.estore.bean.OrderItem;
import com.briup.estore.bean.Shopcar;
import com.briup.estore.bean.ext.OrderExtend;
import com.briup.estore.dto.OrderCreateDTO;
import com.briup.estore.dto.OrderPaySuccessDTO;
import com.briup.estore.dto.OrderSearchDTO;
import com.briup.estore.mapper.BookMapper;
import com.briup.estore.mapper.OrderItemMapper;
import com.briup.estore.mapper.OrderMapper;
import com.briup.estore.mapper.ShopCarMapper;
import com.briup.estore.utils.BeanCopyUtils;
import com.briup.estore.vo.OrderDetailVO;
import com.briup.estore.vo.OrderInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopCarMapper shopCarMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private BookMapper bookMapper;

    @Transactional
    public void createOrder(OrderCreateDTO orderCreateDTO, int userId) {
        //1.添加订单
        //接收到的数据放到order中
        Order order = BeanCopyUtils.copyBean(orderCreateDTO, Order.class);
        //1.1设置订单所属用户（将来添加使用）
        order.setUserId(userId);
        orderMapper.save(order);//添加订单
        //2.添加订单项 一个订单有多个订单项
        List<Integer> shopCarIds = orderCreateDTO.getShopCarIds();
        for (Integer shopCarId : shopCarIds) {
            //2.1查出每一行的购物车
            Shopcar shopcar = shopCarMapper.findById(shopCarId);
            //2.2将每行的购物车 转化为 订单项
            OrderItem orderItem = BeanCopyUtils.copyBean(shopcar, OrderItem.class);
            orderItem.setOrderId(order.getId());
            //2.3添加订单项
            orderItemMapper.saveOrderItem(orderItem);
            //3.移除购物车
            shopCarMapper.deleteById(shopCarId);
            //4.减库存
            bookMapper.updateStoreNumById(orderItem.getBookId(), orderItem.getNum());
        }
    }

    public List<OrderInfoVO> getOrder(int userId, OrderSearchDTO orderSearchDTO) {
        //订单表
        //用户表
        //订单项（计算   所有订单项的价格和数量）
        //书籍表
        //1.返回结果：四张表的数据的返回（没有总价格和总数量）
        List<OrderExtend> list = orderMapper.selectOrderDetailByUserId(userId, orderSearchDTO);
        //2.计算总价格和总数量
        for (OrderExtend orderExtend : list) orderExtend.genPrice();
        //3.最终返回 List<OrderInfoVO>
        List<OrderInfoVO> vos = BeanCopyUtils.copyBeanList(list, OrderInfoVO.class);
        return vos;
    }

    public void paySuccess(OrderPaySuccessDTO paySuccessDTO) {
        orderMapper.paySuccess(paySuccessDTO);
    }


    public OrderDetailVO orderMessage(String orderId) {
        Order order = orderMapper.orderMessage(orderId);
        OrderDetailVO vos =BeanCopyUtils.copyBean(order,OrderDetailVO.class);
        return vos;
    }


    public void completeOrder(String orderId) {
        orderMapper.updateOrderShou(orderId);
    }
}
